<?php
// ----------------------------------------------------------------------
// pn-commerce Shopping Cart Solution for PostNuke
// http://pncommerce.de
// http://www.sourceforge.net/projects/pn-commerce/
//
// It is based on PostKart 0.701QA from
//			Destin LeBlanc [ postkart@zenflesh.com ]
// ----------------------------------------------------------------------
// Source file:  $Source: /cvsroot/pn-commerce/pn-commerce/pnsmtestapi.php,v $
//
// Last changes from:  $Author: landseer $
//
// Revision:           $Revision: 1.25 $
//
// Date of revision    $Date: 2004/09/12 15:17:32 $
//
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------

require_once("modules/pncommerce/common.php");

/**
 * load the config array and return it
 * no arguments needed
 */
function pncommerce_smtestapi_getConfig ($args=array())
{
  $data=pnModGetVar ('pncommerce', 'smtestData');
  if(isset($data)) return unserialize($data);
  else return false;
}

/**
 * save the config array
 * @param $args array of config values
 */
function pncommerce_smtestapi_setConfig ($args)
{
  pnModSetVar ('pncommerce', 'smtestData',serialize($args));
}



/**
 * You ask the user for some input here, or redirect him to the next stage.
 * *******
 * You have to redirect to 'checkout' when you are ready!
 * *******
 */
function pncommerce_smtestapi_getdata($args)
{
	// no need to ask for anything, just forward
	pnRedirect( pnModURL( 'pncommerce', 'user', 'checkout' ) );
	return true;
}

/**
 * This function is not always necessary, it's just here for templating purposes
 * You can do something with the data entered in getData here
 * *******
 * You have to redirect to 'checkout' when you are ready!
 * *******
 * @param $args['KID']   CustomerID
 * @param $args['BPM'] PayMethod
 * @param $args['BSM'] Shippingmethod
 */
function pncommerce_smtestapi_getdataresponse($args)
{
}

/*
 * returns the footermessage of the chosen SM
  *@returns string
 */
function pncommerce_smtestapi_footermsg ($args)
{
	$config=pncommerce_smtestapi_getConfig();
	return "TestShippingMethod";
}

/**
 * This function get's called after the user has agreed to buy the items in his cart.
 * You can calculate show informations here or ask for data to finish the shippingprocess
 * *******
 * You have to redirect to 'receipt' when you are ready!
 * *******
 */
function pncommerce_smtestapi_processshipping ($args)
{
	pnRedirect( pnModURL( 'pncommerce', 'user', 'receipt' ) );
	return true;
}

/**
 * Asks the user if the order (remove,activate, deactivate) should be processed
 * @param $args['Mode']  'install','remove','activate','deactivate'
 * @return String 
 * @returns A Errorpage or the "Are you sure"  page
 */
function pncommerce_smtestapi_configintro()
{
	$Mode = pnVarCleanFromInput ('Mode');
	if (!pnModAPILoad('pncommerce', 'user'))
    {
        return showerrorpage("unable to load pnuserapi", __FILE__, __LINE__);
    }
	if (pnSecAuthAction(0, '::', '::', ACCESS_ADMIN))
	{
	    switch( $Mode )
	    {
		    case 'install':
    			pnModSetVar ('pncommerce', 'smtest', 'on');
		        break;
		    case 'remove':
			    pncommerce_smtestapi_quietremove();
		        break;
		    case 'activate':
    			pnModSetVar ('pncommerce', 'smtest', 'on');
		        break;
		    case 'deactivate':
			    pnModSetVar ('pncommerce', 'smtest', 'off');
		        break;
		    default:
		        break;
		}
       	pnRedirect( pnModAPIFunc('pncommerce','user','pncModURL',
								array('Module'=>'pncommerce', 'Section'=> 'admin', 'Function'=>'shippingmethodconfig')));
        return true;
    }
	else
	{
		return showerrorpage( _PNC_NOAUTH, __FILE__, __LINE__ );
	}
}

/**
 * Shows the code for the radio-buttoned list of all available shipping methods
 * @returns String
 */
function pncommerce_smtestapi_showoption($args)
{
	extract ($args);
	$temp ="<input type='radio' name='ShippingMethod' value='smtest'";
	if ( $BSM == 'smtest' )
	{
		$temp .= " checked ";
	}
	$temp .= ">"."Just a testshippingmethod"."<br>";
	return $temp;
}

/**
 * returns the descriptive name of the shippingmethod
 * @returns string
 */
function pncommerce_smtestapi_info ()
{
	return ("test Shippingmethod");
}

/**
 * removes all config-data of the shippingmethod in the DB and it's status using
 * pnModDelVar ('pncommerce', 'smtest');	
 */
function pncommerce_smtestapi_quietremove ()
{
	pnModDelVar ('pncommerce', 'smtest');	
	pnModDelVar ('pncommerce', 'smtestData');	
	return true;
}

/**
 * Asks for special data for this shipping method, for example minimum fee or default fee, ...
 * ****
 * You have to redirect to 'shippingmethodconfig' when you're done
 * ****
 * @return String 
 * @returns the config page for this Shiping method
 */ 
function pncommerce_smtestapi_config()
{
	if (!pnModAPILoad('pncommerce', 'user'))
    {
        return showerrorpage("unable to load pnuserapi", __FILE__, __LINE__);
    }
	if (pnSecAuthAction(0, '::', '::', ACCESS_ADMIN))
	{
	    
        $Submit = pnVarCleanFromInput( 'Submit' );
        $smarty = new pnRender();
        $smarty->caching = false;
        
        if( !$Submit )
        {
            $smarty->assign( 'config', pncommerce_smtestapi_getConfig() );
            return $smarty->fetch( 'pncommerce_smtest_configuration.html' );
	    }
	    else // submit is set
	    {
    
    if( !pnSecConfirmAuthKey() ) 
    { 
        return showerrorpage( _PNC_BADAUTHKEY, __FILE__, __LINE__ ); 
    }     
    
    		$storage=array();
    		//PMCalcFunc because I first designed it for PM's
    		$storage['PMCalcFunc'] = pnVarCleanFromInput('PMCalcFunc');
    		$storage=pnModAPIFunc('pncommerce','user','gatherPMCalcData',$storage);
    		pncommerce_smtestapi_setConfig($storage);
    		pnRedirect( pnModAPIFunc('pncommerce','user','pncModURL',
    						array('Module'=>'pncommerce', 'Section'=> 'admin', 'Function'=>'shippingmethodconfig')));
    		return true; 
	    }
	}
    return false;
}

/**
 * Calculates the shipping fee based on all prior given data
 * @param $args['KID']  'install','remove','activate','deactivate'
 * @param $args['WeightTotal']  double total weight of the order
 * @param $args['ItemsTotal']  double total amount of items in the order
 * @param $args['Subtotal']  double total amount of the itemcosts
 * @return Double 
 * @returns the resulting fee 
 */
function pncommerce_smtestapi_getShippingTotal($args)
{
//Return 0 = this Paymethod is for free
	if (!pnModAPILoad('pncommerce', 'user'))
      {
        return showerrorpage("unable to load pnuserapi", __FILE__, __LINE__);
      }
	$config=pncommerce_smtestapi_getConfig();
	//add value to array to make the function able to calculate the fee
	$config['Value']=$args['SubTotal'];
	return pnModAPIFunc('pncommerce','user','getPMCalcValue',$config);
}


?>
